package com.java110.mall.goods.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.Environment;
import com.java110.dto.label.LabelDto;
import com.java110.mall.goods.bmo.label.IDeleteLabelBMO;
import com.java110.mall.goods.bmo.label.IGetLabelBMO;
import com.java110.mall.goods.bmo.label.ISaveLabelBMO;
import com.java110.mall.goods.bmo.label.IUpdateLabelBMO;
import com.java110.po.label.LabelPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/label")
public class LabelApi {

    @Autowired
    private ISaveLabelBMO saveLabelBMOImpl;
    @Autowired
    private IUpdateLabelBMO updateLabelBMOImpl;
    @Autowired
    private IDeleteLabelBMO deleteLabelBMOImpl;

    @Autowired
    private IGetLabelBMO getLabelBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /label/saveLabel
     * @path /app/label/saveLabel
     */
    @RequestMapping(value = "/saveLabel", method = RequestMethod.POST)
    public ResponseEntity<String> saveLabel(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "shopId", "商铺不能为空");
        Assert.hasKeyAndValue(reqJson, "labelName", "请求报文中未包含labelName");
        Assert.hasKeyAndValue(reqJson, "domain", "请求报文中未包含domain");


        LabelPo labelPo = BeanConvertUtil.covertBean(reqJson, LabelPo.class);
        return saveLabelBMOImpl.save(labelPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /label/updateLabel
     * @path /app/label/updateLabel
     */
    @RequestMapping(value = "/updateLabel", method = RequestMethod.POST)
    public ResponseEntity<String> updateLabel(@RequestBody JSONObject reqJson) {
        Environment.isDevEnv();
        Assert.hasKeyAndValue(reqJson, "labelName", "请求报文中未包含labelName");
        Assert.hasKeyAndValue(reqJson, "domain", "请求报文中未包含domain");
        Assert.hasKeyAndValue(reqJson, "labelId", "labelId不能为空");


        LabelPo labelPo = BeanConvertUtil.covertBean(reqJson, LabelPo.class);
        return updateLabelBMOImpl.update(labelPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /label/deleteLabel
     * @path /app/label/deleteLabel
     */
    @RequestMapping(value = "/deleteLabel", method = RequestMethod.POST)
    public ResponseEntity<String> deleteLabel(@RequestBody JSONObject reqJson) {
        Environment.isDevEnv();

        Assert.hasKeyAndValue(reqJson, "shopId", "商铺不能为空");

        Assert.hasKeyAndValue(reqJson, "labelId", "labelId不能为空");


        LabelPo labelPo = BeanConvertUtil.covertBean(reqJson, LabelPo.class);
        return deleteLabelBMOImpl.delete(labelPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param shopId 商铺ID
     * @return
     * @serviceCode /label/queryLabel
     * @path /app/label/queryLabel
     */
    @RequestMapping(value = "/queryLabel", method = RequestMethod.GET)
    public ResponseEntity<String> queryLabel(@RequestParam(value = "shopId") String shopId,
                                             @RequestParam(value = "hasSystem", required = false) String hasSystem,
                                             @RequestParam(value = "labelName", required = false) String labelName,
                                             @RequestParam(value = "domain", required = false) String domain,
                                             @RequestParam(value = "page") int page,
                                             @RequestParam(value = "row") int row) {
        LabelDto labelDto = new LabelDto();
        labelDto.setPage(page);
        labelDto.setRow(row);
        labelDto.setShopId(shopId);
        labelDto.setHasSystem(hasSystem);
        labelDto.setLabelName(labelName);
        labelDto.setDomain(domain);
        return getLabelBMOImpl.get(labelDto);
    }
}
